package com.zoo.springbootinit.dasource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoo.springbootinit.exception.esdao.PostEsDao;
import com.zoo.springbootinit.model.dto.post.PostQueryRequest;
import com.zoo.springbootinit.model.entity.Post;
import com.zoo.springbootinit.model.vo.PostVO;
import com.zoo.springbootinit.service.PostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PostDataSourceAccessor implements DataSourceAccessor<PostVO> {
    @Autowired
    PostEsDao postEsDao;
    @Autowired
    PostService postService;

    @Override
    public Page<PostVO> getDataSource(String searchText, long pageNum, long pageSize) {
        PostQueryRequest postQueryRequest = new PostQueryRequest();
        postQueryRequest.setSearchText(searchText);
        postQueryRequest.setPageSize(pageSize);
        postQueryRequest.setCurrent(pageNum);
        Page<Post> postPage = postService.searchFromEs(postQueryRequest);
        Page<PostVO> postVOPage = new Page<>();
        List<PostVO> postVOS = new ArrayList<>();
        for (Post post : postPage.getRecords()) {
            PostVO postVO = new PostVO();
            BeanUtils.copyProperties(post, postVO);
            postVOS.add(postVO);
        }
        postVOPage.setRecords(postVOS);
        postVOPage.setCurrent(postPage.getCurrent());
        postVOPage.setSize(postPage.getSize());
        postVOPage.setTotal(postPage.getTotal());
        return postVOPage;
    }

    //postService.searchFromEs();
}


//        List<PostEsDTO> postEsDTOS = postEsDao.findByTitle(searchText);
//        List<PostVO> postVOS = new ArrayList<>();
//        for (PostEsDTO postEsDTO : postEsDTOS) {
//            PostVO postVO = new PostVO();
////            postVO.setId(postEsDTO.getId());
//            postVO.setTitle(postEsDTO.getTitle());
//            postVO.setContent(postEsDTO.getContent());
//            postVO.setThumbNum(postEsDTO.getThumbNum());
//            postVO.setFavourNum(postEsDTO.getFavourNum());
//            postVO.setUserId(postEsDTO.getUserId());
//            postVO.setCreateTime(postEsDTO.getCreateTime());
//            postVO.setUpdateTime(postEsDTO.getUpdateTime());
//            postVO.setTagList(postEsDTO.getTags());
//            postVOS.add(postVO);
//        }
//        Page<PostVO> PostVOPage = new Page<>(pageNum, pageSize);
//        PostVOPage.setRecords(postVOS);
//        return PostVOPage;


